<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/2/25
 * Time: 11:41
 */

namespace common\logics\operatorlog;

use Yii;
use common\widgets\Tool;
use yii\base\BaseObject;

class OperatorLogLogic extends BaseObject implements IOperatorLog
{
    /** @var \common\sources\write\operatorlog\OperatorLogAccess $WOperatorLogAccess **/
    private $WOperatorLogAccess;
    /** @var \common\sources\read\operatorlog\OperatorLogAccess $ROperatorLogAccess **/
    private $ROperatorLogAccess;

    public function init()
    {
        $this->WOperatorLogAccess = Yii::$container->get("WOperatorLogAccess");
        $this->ROperatorLogAccess = Yii::$container->get("ROperatorLogAccess");
        parent::init(); // TODO: Change the autogenerated stub
    }
    //操作日志
    public function addOperatorLog($data)
    {
        if(empty($data['username']))
        {
            return Tool::returnJson([],401,"用户名不能为空");
        }
        if(empty($data['login_ip']))
        {
            return Tool::returnJson([],402,"登陆ip不能为空");
        }
        if(empty($data['content']))
        {
            return Tool::returnJson([],403,"操作描述不能为空");
        }
        if(empty($data['operator_phone']))
        {
            return Tool::returnJson([],405,"操作人手机不能为空");
        }
        $result = $this->WOperatorLogAccess->addOperatorLog($data);
        if($result)
        {
            return Tool::returnJson([],200,"成功");
        }
        return Tool::returnJson([],406,"添加日志失败");
    }

    public function getList($data)
    {
        $page_num = $data['page_num'];
        if(!$page_num)
        {
            $page_num = 1;
        }
        $count = $this->ROperatorLogAccess->getCount($data);
        $list = [];
        if($count >0)
        {
            $list = $this->ROperatorLogAccess->getList($data);
            foreach ($list as &$item)
            {
                $item['create_time'] = isset($item['create_time']) ? date('Y-m-d H:i:s',$item['create_time']) : '';
            }
        }
        return Tool::returnJson([
            "list" => $list,
            "page_num" => $page_num,
            "limit" => $data['limit'],
            "count" => $count
        ],200,"成功");
    }

    //添加导出日志
    public function setExportLog($data)
    {
        if(empty($data['username']))
        {
            return Tool::returnJson([],401,"用户名不能为空");
        }
        if(empty($data['login_ip']))
        {
            return Tool::returnJson([],402,"登陆ip不能为空");
        }
        if(empty($data['content']))
        {
            return Tool::returnJson([],403,"操作描述不能为空");
        }
        if(empty($data['operator_phone']))
        {
            return Tool::returnJson([],405,"操作人手机不能为空");
        }
        if(empty($data['export']))
        {
            return Tool::returnJson([],405,"请确定是否为导出操作");
        }
        if(empty($data['export_time']))
        {
            return Tool::returnJson([],405,"导出时间为空");
        }
        $result = $this->WOperatorLogAccess->setExportLog($data);
        if($result)
        {
            return Tool::returnJson([],200,"成功");
        }
        return Tool::returnJson([],406,"添加日志失败");
    }

    public function GetExportLog($data) {
        $page_num = $data['page_num'];
        if(!$page_num)
        {
            $page_num = 1;
        }
        $count = $this->ROperatorLogAccess->countExportLog($data);
        $list = $this->ROperatorLogAccess->GetExportLog($data);
        return Tool::returnJson([
            "list" => $list,
            "page_num" => $page_num,
            "limit" => $data['limit'],
            "count" => $count,
        ],200,"成功");
    }
}